import { request } from "../../request/index.js";
Page({

  data: {
leftlist:[],
rightlist:[],
currentIndex: 0,
// 右侧内容的滚动条距离顶部的距离
scrollTop: 0
  },
Cates:[],
  onLoad: function (options) {
    const Cates = wx.getStorageSync("cates");
    // 2 判断
    if (!Cates) {
      // 不存在  发送请求获取数据
      this.getcatgory();
    } else {
      // 有旧的数据 定义过期时间  10s 改成 5分钟
      if (Date.now() - Cates.time > 1000 * 10) {
        // 重新发送请求
        this.getcatgory();
      } else {
        // 可以使用旧的数据
        this.Cates = Cates.data;
        let leftlist = this.Cates.map(v => v.cat_name);
        let rightlist = this.Cates[0].children;
        this.setData({
          leftlist,
          rightlist
        })
      }
    }
  },

  getcatgory(){
    request({
      url:"/categories"
    }).then(res=>{
   this.Cates=res
   let leftlist=this.Cates.map(v=>v.cat_name)
   let rightlist=this.Cates[0].children
   this.setData({
     leftlist,
     rightlist
   })
    })
  },
  handleItemTap(e) {

    const { index } = e.currentTarget.dataset;
    let rightlist = this.Cates[index].children;
    this.setData({
      currentIndex: index,
      rightlist,
      scrollTop: 0
    })

  }

})